var roleE9S57_HBR_I = {
    run: function(creep) {

        //creep.say(creep.memory.building);

        if(creep.store.getUsedCapacity()==0){
            creep.memory.building = true;
        }
        if(creep.store.getFreeCapacity()==0){
            creep.memory.building = false;
        }


        if(creep.memory.building == true){
            if(creep.room.name != 'E9S57'){
                //creep.say(321);
                creep.moveTo(Game.flags['E9S57'].pos, {reusePath:10});
            }
            else{
                //creep.say(321321);
                var sources = creep.room.find(FIND_SOURCES);
                if(creep.harvest(sources[1]) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(sources[1], {reusePath:10});
                }
            }
        }
        else{     
            //建设           
            var targetsbuild = creep.room.find(FIND_CONSTRUCTION_SITES);
            if(targetsbuild.length > 0 && creep.room.name != 'E9S58') {
                if(creep.build(targetsbuild[0]) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(targetsbuild[0], {reusePath:10});
                    //creep.say('建去');    //说话测试
                }
            }
            //修理
            else{
                var targetsfix = creep.room.find(FIND_STRUCTURES, {
                    filter: (structure) => structure.hits < structure.hitsMax*0.08
                });
                targetsfix.sort((a,b) => a.hits - b.hits);
                if(targetsfix.length > 0 && creep.room.name != 'E9S58') {
                    if(creep.repair(targetsfix[0]) == ERR_NOT_IN_RANGE) {
                        creep.moveTo(targetsfix[0] ,{reusePath:10});
                        //creep.say('修去');
                    }
                }
                //运矿回去
                else{
                    if(creep.room.name != 'E9S58'){
                        //creep.say(321);
                        creep.moveTo(Game.flags['E9S58'].pos, {reusePath:10});
                    }
                    else{
                        var targetsstorage = creep.room.find(FIND_STRUCTURES, {
                            filter: (structure) => {
                                return (structure.structureType == STRUCTURE_STORAGE) && 
                                structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
                            }
                        });
                        if(targetsstorage.length > 0) {
                            if(creep.transfer(targetsstorage[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                                creep.moveTo(targetsstorage[0], {reusePath:10});
                                //creep.say('搬去');
                            }
                        }
                    }
                }
            }
        }
	}
};

module.exports = roleE9S57_HBR_I;